﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Win32;

namespace ChatBox.Utilities
{
  /// <summary>
  /// Represents a dialog service that is used to show dialogs and make sure the
  /// new dialogs have their Owner property set correctly.
  /// </summary>
  public class DialogService : IDialogService
  {
    #region Constructors

    /// <summary>
    /// Initializes a new dialog service with the given owner to use when
    /// showing dialogs.
    /// </summary>
    /// <param name="_owner">Owner to give to the shown dialogs. Can be null
    /// if the window doesn't have any owner or if it can be determined
    /// automatically.</param>
    public DialogService( Window owner )
    {
      this._owner = owner;
    }

    #endregion

    #region Private Variables

    private readonly Window _owner;

    #endregion

    #region Private Static Methods

    #endregion

    #region Public Static Methods

    /// <summary>
    /// Opens a window with the given view model and returns only when the newly
    /// opened window is closed.
    /// </summary>
    /// <typeparam name="T">Type of the window to show.</typeparam>
    /// <param name="_owner">Owner to set to the dialog to show.</param>
    /// <param name="_viewModel">View model to give to the dialog to show. It
    /// will have its DialogService property set.</param>
    /// <returns>A <see cref="System.Nullable`1"/> value of type <see cref="T:System.Boolean"/> that signifies how the window was closed by the user.</returns>
    public static Boolean? ShowDialog<T>( Window owner, IDialogServiceClient viewModel ) where T : Window, new()
    {
      T view = new T() { Owner = owner, DataContext = viewModel };

      viewModel.DialogService = new DialogService( view );

      return view.ShowDialog();
    }

    /// <summary>
    /// Opens a window and returns without waiting for the newly opened window
    /// to close.
    /// </summary>
    /// <typeparam name="T">Type of the window to show.</typeparam>
    /// <param name="_viewModel">View model to give to the window to show. It
    /// will have its DialogService property set.</param>
    public static T Show<T>( IDialogServiceClient viewModel ) where T : Window, new()
    {
      T view = new T() { DataContext = viewModel };

      viewModel.DialogService = new DialogService( view );

      view.Show();

      return view;
    }

    #endregion

    #region IDialogService Members

    public Boolean? ShowDialog<T>( Object viewModel ) where T : Window, new()
    {
      T view = new T() { Owner = this._owner, DataContext = viewModel };

      return view.ShowDialog();
    }

    public Boolean? ShowDialog<T>( IDialogServiceClient viewModel ) where T : Window, new()
    {
      return DialogService.ShowDialog<T>( this._owner, viewModel );
    }

    public Boolean? ShowDialog( CommonDialog dialog )
    {
      return dialog.ShowDialog( this._owner );
    }

    #endregion
  }
}
